#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>

int i,n;
int *array;

void append()
{
	int c;
	printf("Enter the element : ");
	scanf("%d",&c);
	n++;
	array = (int *)realloc(array,(n*sizeof(int)));
	array[n-1]=c;
}

void rremove()
{
	printf("Enter the element to be deleted : ");
	int m;
	scanf("%d",&m);
	int node,flag;
	flag=0;
	for(i=0;i<n;i++)
	{
	if(array[i]==m)
		{
			flag=1;
			node=i;
			break;
		}
	}
	if(!flag)
		printf("The element is not present in the List\n");
	else{	
		for(i=node;i<n-1;i++)
		{
			array[i]=array[i+1];
		}
		n--;
		array=(int *)realloc(array,(n*sizeof(int)));
}
}

void indexx()
{
	printf("Enter the element whose index you want to find : ");
	int a;
	scanf("%d",&a);
	int pos,flag=0;
	for(i=0;i<n;i++)
	{
	if(array[i]==a)
		{
		pos=i+1;
		flag=1;
		break;
		}
	}
	if(!flag)
		printf("No such element in the list.\n");
	else
	printf("The element %d is at %d position in the list.\n",a,pos);
}

void sort()
{
	int a,b;
	for(i=0;i<n;i++)
	{
		for(a=0;a<n;a++)
		{
			if(array[a]>array[a+1])
			{
				b=array[a+1];
				array[a+1]=array[a];
				array[a]=b;	
			}
		}
	}
}

void reverse()
{
	int a,b;
	for(i=0,a=n-1;i<a;i++,a--)
	{
		b=array[i];
		array[i]=array[a];
		array[a]=b;
	}
}

void extend()
{
	int k;
	printf("Enter the list size to be extended\n");
	scanf("%d",&k);
	int m[k];
	i=0;
	while(i<k) {
		printf("Enter %d element: ", i);
		scanf("%d", &m[i]);
		i++;
	}
	array=(int *)realloc(array,((n+k)*sizeof(int)));

	for(i=0;i<k;i++)
	{
		array[n++]=m[i];
	}
}

void insert()
{
	int m,p;
	printf("Enter the position to be inserted with the element\n");
	int flag;
	flag=0;
	while(!flag)
	{
		scanf("%d",&m);
		flag=1;
		if(m>n || m<1)
		{	
			printf("Invalid index.\nTry again !!!\n");
			flag=0;	
		}
	}	
	printf("Enter the value of the element to be inserted.\n");
	scanf("%d",&p);
	n++;
	array=(int *)realloc(array,(n*sizeof(int)));
	int a[n-m];
	for(i=0;i<(n-m);i++)
	{
		a[i]=array[m-1+i];
	}
	array[m-1]=p;
	for(i=0;i<(n-m);i++)
	{
		array[m+i]=a[i];
	}
}

void count()
{
	printf("Enter the element to be counted\n");
	int m;
	scanf("%d",&m);
	int count=0;
	for(i=0;i<n;i++)
	{
		if(array[i]==m)
		count++;
	}
	printf("The element %d appears %d times.\n",m,count);
}

void pop()
{
	printf("Enter the index of the element to be poped out\n");
	int m;
	int flag;
	flag=0;
	while(!flag)
	{
		scanf("%d",&m);
		if(m>n || m<1)
			printf("Invalid index.\nTry again!!!!\n");
		else
			flag=1;
	}
	for(i=m-1;i<n-1;i++)
		array[i]=array[i+1];
		n--;
	array=(int *)realloc(array,(n*sizeof(int)));
}

void main() {

	int a,flag=1;
	printf("\n\nEnter the number of elements (size of list): ");
	scanf("%d", &n);
	array = (int*)malloc((n+1)*sizeof(int));
	i=0;
	while(i<n) {
		printf("Enter %d element: ", i);
		scanf("%d", &array[i]);
		i++;
	}

	printf("\nThe array is : [ ");
	for (i=0; i<n; i++) {
		printf("%d",array[i]);
		if(i<n-1)
			printf(",");	
	}
	printf(" ]\n");
//	printf("Size of The array = %d\n", i);
	
	while(flag){
		printf("\n-------------Enter your option--------------");
		printf("\n\t1.Append\n\t2.Remove\n\t3.Index\n\t4.Sort\n\t5.Reverse\n\t6.Extend\n\t7.Insert\n\t8.Count\n\t9.Pop");
		printf("\n\nEnter here : ");
		scanf("%d",&a);

	if(a==1)
	{
		append();
	}
	if(a==2)
		rremove();
	if(a==3)
		indexx();
	if(a==4)
		sort();
	if(a==5)
		reverse();
	if(a==6)
		extend();
	if(a==7)
		insert();
	if(a==8)
		count();
	if(a==9)
		pop();
	if(a!=3 && a!=8)
		{
			printf("\n---------The new List is----------\n");
			printf("[ ");	
			for (i=0; i<n; i++) {
				printf("%d",array[i]);
				if(i<n-1)
					printf(",");	
			}
	printf(" ]\n");
		}
	printf("\nType 'y' to continue and 'n' to terminate\n");
	char b;	
	scanf("%s",&b);
	if(b!='y')
	flag=0;
	}
	{printf("\n---------The new List is----------\n");
			printf("[ ");	
			for (i=0; i<n; i++) {
				printf("%d",array[i]);
				if(i<n-1)
					printf(",");	
			}
	printf(" ]\n");}
	free(array);
}
